Package org.javacommerce.paypal.servlet

Source Code of org.javacommerce.paypal.servlet.BaseServlet

/**
*
*/
package org.javacommerce.paypal.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import org.javacommerce.paypal.APIUtil;
import org.javacommerce.paypal.PayPalError;
import org.javacommerce.paypal.ws.API;
import org.javacommerce.paypal.ws.APICredential;
import org.javacommerce.paypal.ws.APIException;

/**
* @author Michael Blanton (mike@mikeblanton.com)
*/
public abstract class BaseServlet extends HttpServlet {

  private static final Log LOG = LogFactory.getLog(BaseServlet.class);
  protected static final int SC_METHOD_NOT_ALLOWED = 405;
  protected static final int SC_NOT_ACCEPTABLE = 406;

  /*
   * (non-Javadoc)
   *
   * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
   *      javax.servlet.http.HttpServletResponse)
   */
  protected abstract void doGet(HttpServletRequest arg0,
      HttpServletResponse arg1) throws ServletException, IOException;

  /*
   * (non-Javadoc)
   *
   * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
   *      javax.servlet.http.HttpServletResponse)
   */
  protected abstract void doPost(HttpServletRequest arg0,
      HttpServletResponse arg1) throws ServletException, IOException;

  /*
   * (non-Javadoc)
   *
   * @see javax.servlet.http.HttpServlet#doPut(javax.servlet.http.HttpServletRequest,
   *      javax.servlet.http.HttpServletResponse)
   */
  protected abstract void doPut(HttpServletRequest arg0,
      HttpServletResponse arg1) throws ServletException, IOException;

  protected final void writeObject(Object _object, HttpServletResponse _response)
      throws ServletException {
    try {
      APIUtil.writeXML(_object, _response);
    } catch (MarshalException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("Error writing " + _object.getClass().getName()
            + " XML: " + e.getLocalizedMessage(), e);
      }
      throw new ServletException(e);
    } catch (ValidationException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("Error writing " + _object.getClass().getName()
            + " XML: " + e.getLocalizedMessage(), e);
      }
      throw new ServletException(e);
    } catch (IOException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("Error writing " + _object.getClass().getName()
            + " XML: " + e.getLocalizedMessage(), e);
      }
      throw new ServletException(e);
    } catch (MappingException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("Error writing " + _object.getClass().getName()
            + " XML: " + e.getLocalizedMessage(), e);
      }
      throw new ServletException(e);
    }
  }
 
  protected final void writeException(String _errorCode, String _message,
      APICredential _credentials, Exception _exception,
      HttpServletResponse _response) throws ServletException {
    try {
      APIUtil.writeXML(new PayPalError(_errorCode, _message,
          _credentials, _exception), _response);
    } catch (MarshalException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error(
            "Error writing error XML: " + e.getLocalizedMessage(),
            e);
      }
      throw new ServletException(e);
    } catch (ValidationException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error(
            "Error writing error XML: " + e.getLocalizedMessage(),
            e);
      }
      throw new ServletException(e);
    } catch (IOException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error(
            "Error writing error XML: " + e.getLocalizedMessage(),
            e);
      }
      throw new ServletException(e);
    } catch (MappingException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error(
            "Error writing error XML: " + e.getLocalizedMessage(),
            e);
      }
      throw new ServletException(e);
    }
  }

  protected final void notAllowed(HttpServletResponse _response, String _accept) {
    _response.addHeader("Accept", _accept);
    _response.setStatus(SC_METHOD_NOT_ALLOWED);
  }
 
  protected APICredential getAPICredentials(HttpServletRequest _request) throws APIException {
    return API.DEFAULT_CREDENTIALS;
  }

}
TOP

Related Classes of org.javacommerce.paypal.servlet.BaseServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.